<template>
  <!-- 没有商品的购物车 -->
  <view class="empty-cart" v-if="cart.length == 0">
    <image src="../../static/empty-cart.webp" class="empty-img"></image>
    <text>购物车居然空空如也</text>
  </view>
  
  <!-- 有商品的购物车 -->
  <view v-else>
    <!-- 收货地址 -->
    <view class="address-box">
      <my-address></my-address>
    </view>
    
    <!-- 标题区域 -->
    <view class="cart-title">
      <uni-icons type="shop" size="23" ></uni-icons>
      <text>购物车</text>
    </view>
  
    <!-- 购物车列表 -->
    <uni-swipe-action>
      <block v-for="goods in cart" :key="goods.goods_id">
        <uni-swipe-action-item :right-options="options" @click="removeGoods(goods)">
          <my-goods :item="goods" :showRadio="true" @radioChangeHandler="radioChangeHandler" :showNum="true" @numChangeHandler="numChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
  
    <!-- 结算区 -->
    <my-sittle></my-sittle>
  </view>
</template>

<script>
  import cartBadge from '@/mixins/cart-badge.js'
  import { mapState, mapMutations} from 'vuex'
  export default {
    mixins: [cartBadge],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }],
      };
    },
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    methods: {
      ...mapMutations('m_cart', ['cartCheckChange', 'updataGoodsCount', 'removeCartGoods']),
      // 将购物车商品选中状态更新到内存
      radioChangeHandler(e) {
        this.cartCheckChange(e)
      },
      // 将购物车的商品数量更新到内存
      numChangeHandler(e) {
        this.updataGoodsCount(e)
      },
      // 删除商品
      removeGoods(goods) {
        this.removeCartGoods(goods)
      }
    }
  }
</script>

<style lang="scss">
.cart-title {
  line-height: 60rpx;
  display: flex;
  align-content: center;
  border-bottom: 1px solid #efefef;
  .uni-icons {
    margin: 0 10rpx;
  }
}
.empty-cart {
  height: 1110rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #ffffff;
  
  .empty-img {
    margin-top: 300rpx;
    width: 300rpx;
    height: 300rpx;
  }
}
</style>
